﻿using System;
using ProtoBuf;
using System.Collections.Generic;

namespace ZyGames.DZPocker.Model
{
	/// <summary>
	/// 玩家属性
	/// </summary>
	[Serializable, ProtoContract]
	public class UserProperty : ZyGames.Framework.Event.EntityChangeEvent
	{
		public UserProperty()
		{
		}

		[ProtoMember(1)]
		public int GameId { get; set; }

		[ProtoMember(2)]
		public int ServerId { get; set; }

		/// <summary>
		/// 是否加载中
		/// </summary>
		[ProtoMember(3)]
		public bool IsRefreshing { get; set; }

		/// <summary>
		/// 玩家聊天时间
		/// </summary>
		[ProtoMember(4)]
		public DateTime ChatDate { get; set; }

		/// <summary>
		/// [临时缓存]聊天版本
		/// </summary>
		[ProtoMember(5)]
		public int ChatVesion { get; set; }

		/// <summary>
		/// [临时缓存]广播版本
		/// </summary>
		[ProtoMember(6)]
		public int BroadcastVesion { get; set; }

		/// <summary>
		/// [临时缓存]房间ID
		/// </summary>
		[ProtoMember(7)]
		public int RoomId { get; set; }

		/// <summary>
		/// [临时缓存]当前德州局数
		/// </summary>
		[ProtoMember(10)]
		public int InningNum { get; set; }

		/// <summary>
		/// 初始化桌子
		/// </summary>
		public void InitTableInfo(int roomid = 0, int tableId = 0)
		{
			RoomId = roomid;
			AddTableID(tableId);
		}

		public void AddTableID(int tableId)
		{
			if (!TableIdList.Contains(tableId))
				TableIdList.Add(tableId);
		}

		/// <summary>
		/// [临时缓存]桌子ID
		/// </summary>
		//[ProtoMember(8)]
		public List<int> TableIdList = new List<int>();

		/// <summary>
		/// [临时缓存]桌子座位ID
		/// </summary>
		//[ProtoMember(9)]
		public Dictionary<int, int> PositionId = new Dictionary<int, int>();

		/// <summary>
		/// 玩家进入桌子第一次坐下会弹出桌子规则,该标记记录玩家是否了解房间规则,如果Ready为True，说明对这个房间已经坐下过了  TODO: 如果多桌的话 这个变量有问题要跟桌子绑定
		/// </summary>
		private Dictionary<int, bool> tableReadyState = new Dictionary<int, bool>();

		public bool GetReadyState(int tableId)
		{
			bool isReady;
			if (this.tableReadyState.TryGetValue(tableId, out isReady))
				return isReady;
			else
				return false;
		}

		public void SetReadyState(int tableId, bool ready = true)
		{
			if (this.tableReadyState.ContainsKey(tableId))
				this.tableReadyState[tableId] = ready;
			else
				this.tableReadyState.Add(tableId, ready);
		}

		public void SetTablePositionId(int tableId, int posId)
		{
			if (PositionId.ContainsKey(tableId))
				PositionId[tableId] = posId;
			else
				PositionId.Add(tableId, posId);
		}

		public int GetPositionId(int tableId)
		{
			int posId;
			if (PositionId.TryGetValue(tableId, out posId))
				return posId;
			else
				return 0;
		}

		public void ResetState(int tableId)
		{
			this.SetTablePositionId(tableId, 0);
			Console.WriteLine("重置************************************88");
			this.SetReadyState(tableId, false);
		}

		public void Clear(int tableId)
		{
			Console.WriteLine(string.Format("******************移除Table:{0}", tableId));
			this.TableIdList.Remove(tableId);
			this.SetTablePositionId(tableId, 0);
			this.SetReadyState(tableId, false);
		}
	}
}
